Thread: Checking user input [int/char] (Help)

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Daniell Wang
    This's not use lib , like this:
    Good, but note that while your check for digit is fine, your check for alphabetic characters make an assumption about their values that is not guaranteed to be true.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Dec 2012
    Posts
    16

    Smile

    Quote Originally Posted by Daniell Wang View Post
    This's not use lib , like this:
    Code:
    #include<stdio.h>
    int main(int argc, char *argv[])
    {
        int i;
        printf("Enter something: ");
        i=getchar();
        if(i >= '0' && i <= '9')
            puts("You have entered a number");
        else if((i >='a' && i <= 'z') || (i >='A' && i <= 'Z')) // 
            puts("You have entered an alphabet");
        else
            puts("Invalid input");
    }
    Oh ! I didn't know that, that comparison operators can also be used on characters!

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    EBCDIC - Wikipedia, the free encyclopedia
    And use the character classification functions in ctype.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Daniell Wang View Post
    This's not use lib , like this:
    Code:
    #include<stdio.h>
    int main(int argc, char *argv[])
    {
        int i;
        printf("Enter something: ");
        i=getchar();
        if(i >= '0' && i <= '9')
            puts("You have entered a number");
        else if((i >='a' && i <= 'z') || (i >='A' && i <= 'Z')) // 
            puts("You have entered an alphabet");
        else
            puts("Invalid input");
    }
    Doesn't look like he's expecting any command line arguments so it might be better to simply
    Code:
     int main(void){...}
    As well main always returns an int.
    Last edited by caroundw5h; 12-26-2012 at 06:30 PM. Reason: added {...}
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #20
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by caroundw5h View Post
    Doesn't look like he's expecting any command line arguments so it might be better to simply
    Code:
     int main(void){...}
    As well main always returns an int.
    very useful to me on this forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking User Input
    By Dusterdoo in forum C Programming
    Replies: 9
    Last Post: 06-25-2011, 03:04 PM
  2. Checking the user input.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 05-13-2008, 10:03 PM
  3. Checking user input?
    By hayai32 in forum C Programming
    Replies: 9
    Last Post: 04-03-2005, 05:33 PM
  4. Checking user input
    By Cmuppet in forum C Programming
    Replies: 5
    Last Post: 08-05-2004, 10:32 AM
  5. Error checking user input
    By theharbinger in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2003, 09:57 AM

Tags for this Thread